package com.lb.web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.JSONSerializer;
import com.alibaba.fastjson.serializer.SerializeWriter;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.lb.config.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author user
 */
@Slf4j
@RestController
@RequestMapping
public class PublisherController {
    @Resource
    private RedisUtils redisUtils;

    @GetMapping(value = "pub")
    public String pubMsg() {
        JSONObject object = new JSONObject();
        object.put("id","1001");
        object.put("name", "蓝莲花");
        object.put("school", "苏州大学");
        object.put("age", 25);
        redisUtils.convertAndSend("userInfo", JSON.toJSONString(object));
        log.info("Publisher sendes Topic... ");
        return "success";
    }

    public static final String toJSONString(Object object) {
        SerializeWriter out = new SerializeWriter();

        String var3;
        try {
            JSONSerializer serializer = new JSONSerializer(out);
            serializer.config(SerializerFeature.WriteDateUseDateFormat, true);
            serializer.config(SerializerFeature.DisableCircularReferenceDetect, true);
            serializer.setDateFormat("yyyy-MM-dd HH:mm:ss");
            serializer.write(object);
            var3 = out.toString();
        } finally {
            out.close();
        }

        return var3;
    }
}